<?php
namespace app\admin\controller;


use think\Controller;
use think\Db;
use think\Request;
use think\Session;
class Priv extends Controller  //控制器
{
    /**
     * 权限列表
     */
    public function index()
    {
    	if(!in_array(strrchr(__CLASS__."/".__FUNCTION__,"\\"),Session::get('admin_pri','admin'))){
        	$this->error('你没有权限访问权限列表', url('/main'), []);
        }
        $list = Db::name('rbac_privilege')
            ->field(['id', 'pri_name','controller_name','action_name'])
            ->select();

        //模板
        return view('priv/index', [
            'title' => '权限列表',
            'add' => '添加权限',
            'list' => $list
        ]);
    }

    /**
     * 添加权限页面
     */
    public function create()
    {
    	if(!in_array(strrchr(__CLASS__."/".__FUNCTION__,"\\"),Session::get('admin_pri','admin'))){
        	 $this->error('你没有权限添加权限', url('/main'), []);
        }
        return view('priv/create');
    }


    /**
     * 执行添加
     */
    public function docreate(Request $request)
    {
        $p = $request->post();
         Db::startTrans();
         try{
         	Db::name('rbac_privilege')
            ->insert($p);
            Db::commit();
            return true;
         }catch(Exception $e){
         	Db::rollback();
         	return false;
         }
        
        
		


    }
    /**
     * 执行删除
     */
    public function del(Request $request){
    	if(!in_array(strrchr(__CLASS__."/".__FUNCTION__,"\\"),Session::get('admin_pri','admin'))){
        	return "no pri";
        }
    	$id=$request->get()['id'];
    	
    	Db::startTrans();
    	try{
    		
    		$res=DB::query("select controller_name,action_name from rbac_privilege where id='".$id."'");
    		
    		$res="\\".$res[0]['controller_name']."/".$res[0]['action_name'];
    		Db::name('rbac_privilege')
    	 	->where('id','=',$id)
            ->delete();
           
            
            
        	Db::name('rbac_role_privilege')->where('pri_id',$id)->delete();
        	Db::commit();
        	
        	
        	if(in_array($res,Session::get('admin_pri','admin'))){
        		 $arr=Session::get('admin_pri','admin');
        		 unset($arr[array_search($res,$arr)]);
        		 Session::delete('admin_pri','admin');
        		 session('admin_pri', $arr, 'admin');
        	}
        	
    		return true;
    	}catch(Exception $e){
    		Db::rollback();
    		return false;
    	}
    	
    }
    
    /*
     * 获取详情
     */
    
    public function edit(Request $request){
    	if(!in_array(strrchr(__CLASS__."/".__FUNCTION__,"\\"),Session::get('admin_pri','admin'))){
        	return "no pri";
        }
    	$id=$request->get()['id'];
    	$res=Db::name('rbac_privilege')
    		->where('id','=',$id)
            ->select();
        $data=$res[0];
        
        if(is_null($data)){
        	return false;
        }else{
        	 return json($data);
        }
       
    	
    	
    }
    
    /*
     * 编辑
     */
    
    public function doEdit(Request $request){
    	if(!in_array(strrchr(__CLASS__."/".__FUNCTION__,"\\"),Session::get('admin_pri','admin'))){
        	return "no pri";
        }
    	$p = $request->post();
    	
    	Db::startTrans();
    	try{
    		Db::name('rbac_privilege')->update($p);
    		Db::commit();
    		
    		return true;
    	}catch(Exception $e){
    		Db::rollback();
    		return false;
    	}
        
    	
    }
 }
?>